package com.lry.sso.auth.common.util;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 李瑞益
 * @date 2020-02-14 17:31
 */
public class HttpUtils {

    /**
     * 流写出
     *
     * @param response 输出流对象
     * @param status   状态
     * @param json     写出数据
     */
    public static void writeResponse(HttpServletResponse response, int status, String json) {
        try {
            if (response.isCommitted()) {
                return;
            }
            response.reset();//很重要
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "*");
            response.setContentType("application/json;charset=UTF-8");
            response.setStatus(status);
            response.getWriter().write(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
